package day14.edu;
// Created: 14/09/2025, ChangJiang Ru
public class BankTest {
    static Bank b1;
    static Bank b2;

    public static void main(String[] args) {

        Thread t1 = new Thread(){
            @Override
            public void run()
            {
                b1 = Bank.getInstance();
            }
        };
        Thread t2 = new Thread(){
            @Override
            public void run()
            {
                b2 = Bank.getInstance();
            }
        };

        t1.start();
        t2.start();

        try
        {
        t1.join();
        }catch(InterruptedException e)
        {
            e.printStackTrace();
        }

        try
        {
        t2.join();
        }catch(InterruptedException e)
        {
            e.printStackTrace();
        }

        System.out.println(b1);
        System.out.println(b2);
        System.out.println(b1 == b2);
    }

}

class Bank
{
    private Bank(){

    }

    private static Bank instance = null;

    /* Case 1:
    public static synchronized Bank getInstance()
    {
        try
        {
            Thread.sleep(1000);
        }catch(InterruptedException e)
        {
            e.printStackTrace();
        }

        if(instance == null)
            return new Bank();
        else
            return instance;
    }

     */
    public static  Bank getInstance() {
        if(instance == null) {
            synchronized (Bank.class) {



                if (instance == null) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    instance = new Bank();
                }
            }
        }
        return instance;
    }
}
